#  Project       mcutools
#  (c) copyright 2023.05
#  Company       Autolink
#                All rights reserved
#  Secrecy Level STRICTLY CONFIDENTIAL
#
#        Package  : dbcp
#        Description: dbc parse tool
#        Author: hezy
#        Date  : 2023-05-07

'''
from Platform.dbcp.dbc import *
from Platform.logs.logs import *

if __name__ == '__main__':
    file = "SDB22100_FX11_High_PrivateInfoCAN_220428.dbc"
    log = Logs()
    dbc = DBC(file, 'IHU')

    log.debug('doc', dbc.__doc__)

    # 遍历消息
    for msg in dbc.msgs:
        log.debug('msg:', msg)
        log.debug('msg.name:', msg.name)
        log.debug('msg.idformat:', msg.idformat)
        log.debug('msg.length:', msg.length)
        log.debug('msg.direction:', msg.direction)
        log.debug('msg.comment:', msg.comment)
        log.debug('msg.attribute:', msg.attribute)
        # 遍历信号
        for sig in msg.signals:
            if sig.name == 'BgrndForAudOutpChAudOutpCh7':
                log.debug('sig:', sig)
                log.debug('sig.start_bit:', sig.start_bit)
                log.debug('sig.length_bit:', sig.length_bit)
                log.debug('sig.initial_value:', sig.initial_value)
                log.debug('sig.factor:', sig.factor)
                log.debug('sig.offset:', sig.offset)
                log.debug('sig.unit:', sig.unit)
                log.debug('sig.value_table:', sig.value_table)
                log.debug('sig.comment:', sig.comment)
                log.debug('sig.attribute:', sig.attribute)

    # 检查消息ID是否存在
    if 0x380 in dbc.msgs:
        log.debug('msg 380 exist')
    else:
        log.debug('msg 380 not exist')

    # 检查消息名称是否存在
    if 'AUDInfoCanFr01' in dbc.msgs:
        log.debug('msg AUDInfoCanFr01 exist')
    else:
        log.debug('msg AUDInfoCanFr01 not exist')

    # 根据消息ID获取DbcMessage对象
    log.debug('msgs[0x380]:', dbc.msgs[0x380])
    # 根据消息名称获取DbcMessage对象
    log.debug('msgs[AUDInfoCanFr01]:', dbc.msgs['AUDInfoCanFr01'])

    # 检查消息ID 0x380下指定信号是否存在
    if 'BgrndForAudOutpChAudOutpCh7' in dbc.msgs[0x380].signals:
        log.debug('sig exist')
    else:
        log.debug('sig not exist')

    sigs = dbc.msgs[0x150].signals
    # 根据信号列表索引获取DbcSignal对象
    log.debug('sigs[0]', sigs[0])
    # 根据信号名称获取DbcSignal对象
    log.debug('sigs[BgrndForAudOutpChAudOutpCh7]:', sigs['BgrndForAudOutpChAudOutpCh7'])
    log.debug('0x380 signal[0] value table:', sigs['BgrndForAudOutpChAudOutpCh7'].value_table)
    log.debug('0x380 signal[0] comment', sigs['BgrndForAudOutpChAudOutpCh7'].comment)

    # 遍历signals
    log.debug('loops signals')
    for sig in dbc.sigs:
        if sig.name == 'BgrndForAudOutpChAudOutpCh7':
            log.debug(sig)

    if 'BgrndForAudOutpChAudOutpCh7' in dbc.sigs:
        log.debug('signal BgrndForAudOutpChAudOutpCh7 exist')
        # 获取信号名对应的Signal对象
        sig = dbc.sigs['BgrndForAudOutpChAudOutpCh7']
        # 根据Signal对象中的消息ID获取Message对象
        msg = dbc.msgs[sig.msgid]
        log.debug('msg:', msg)
        log.debug('msg.name:', msg.name)
        log.debug('msg.idformat:', msg.idformat)
        log.debug('msg.length:', msg.length)
        log.debug('msg.direction:', msg.direction)
        log.debug('msg.comment:', msg.comment)
        log.debug('msg.attribute:', msg.attribute)
    else:
        log.debug('signal BgrndForAudOutpChAudOutpCh7 not exist')

'''

from .dbc import *

